
package com.ami.kvm.jviewer;

import com.ami.kvm.jviewer.gui.DebugLogFileChooser;
import com.ami.kvm.jviewer.gui.InfoDialog;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Debug {
	public static Debug out = new Debug();

	public static int MODE = 1;
	public static final int CREATE_LOG = 0;
	public static final int DEBUG = 1;
	public static final int RELEASE = 2;
	private File logFile = null;
	private FileWriter fWriter = null;
	private PrintWriter pWriter = null;

	private boolean runTimeLogging = false;

	private Debug() {
//		MODE = 2;
		if (MODE == 0) {
			initLog(this.logFile);
		}
	}

	public void print(Object paramObject) {
		Object localObject;
		if (MODE == 1) {
			if (paramObject.getClass().getName().contains("Exception")) {
				localObject = (Exception) paramObject;
				System.err.println("\nEXCEPTION !!!");
				((Exception) localObject).printStackTrace();
			} else if (paramObject.getClass().getName().contains("Error")) {
				localObject = (Error) paramObject;
				System.err.println("\nERROR !!!");
				((Error) localObject).printStackTrace();
			} else {
				System.out.print(paramObject);
			}
		} else if (MODE == 0) {
			try {
				if (this.fWriter == null) {
					this.fWriter = new FileWriter(this.logFile, true);
				}
				if (paramObject.getClass().getName().contains("Exception")) {
					localObject = (Exception) paramObject;
					if (this.pWriter == null)
						this.pWriter = new PrintWriter(this.fWriter, true);
					this.pWriter.append("\nEXCEPTION !!!\n");
					((Exception) localObject).printStackTrace(this.pWriter);
					this.pWriter.append('\n');
				} else if (paramObject.getClass().getName().contains("Error")) {
					localObject = (Error) paramObject;
					if (this.pWriter == null)
						this.pWriter = new PrintWriter(this.fWriter, true);
					this.pWriter.append("\nERROR !!!\n");
					((Error) localObject).printStackTrace(this.pWriter);
					this.pWriter.append('\n');
				} else {
					this.fWriter.append((CharSequence) paramObject);
				}
			} catch (IOException localIOException1) {
				System.err.println("ERROR WHILE WRITTING TO LOG FILE!!!");
				localIOException1.printStackTrace();
				closeLog();
			} catch (ClassCastException localClassCastException) {
				try {
					this.fWriter.append(paramObject.toString());
				} catch (IOException localIOException2) {
					System.err.println("ERROR WHILE WRITTING TO LOG FILE!!!");
					localIOException2.printStackTrace();
					closeLog();
				}
			} catch (Exception localException) {
				System.err.println("ERROR WHILE WRITTING TO LOG FILE!!!");
				localException.printStackTrace();
				closeLog();
			}
		}
	}

	public void println(Object paramObject) {
		Object localObject;
		if (MODE == 1) {
			if (paramObject.getClass().getName().contains("Exception")) {
				localObject = (Exception) paramObject;
				System.err.println("\nEXCEPTION !!!");
				((Exception) localObject).printStackTrace();
			} else if (paramObject.getClass().getName().contains("Error")) {
				localObject = (Error) paramObject;
				System.err.println("\nERROR !!!");
				((Error) localObject).printStackTrace();
			} else {
				System.out.println(paramObject);
			}
		} else if (MODE == 0) {
			try {
				if (this.fWriter == null) {
					this.fWriter = new FileWriter(this.logFile, true);
				}
				if (paramObject.getClass().getName().contains("Exception")) {
					localObject = (Exception) paramObject;
					if (this.pWriter == null)
						this.pWriter = new PrintWriter(this.fWriter, true);
					this.pWriter.append("\nEXCEPTION !!!\n");
					((Exception) localObject).printStackTrace(this.pWriter);
					this.pWriter.append('\n');
				} else if (paramObject.getClass().getName().contains("Error")) {
					localObject = (Error) paramObject;
					if (this.pWriter == null)
						this.pWriter = new PrintWriter(this.fWriter, true);
					this.pWriter.append("\nERROR !!!\n");
					((Error) localObject).printStackTrace(this.pWriter);
					this.pWriter.append('\n');
				} else {
					this.fWriter.append((CharSequence) paramObject);
					this.fWriter.append('\n');
				}
			} catch (IOException localIOException1) {
				System.err.println("ERROR WHILE WRITTING TO LOG FILE!!!");
				localIOException1.printStackTrace();
				closeLog();
			} catch (ClassCastException localClassCastException) {
				try {
					this.fWriter.append(paramObject.toString());
				} catch (IOException localIOException2) {
					System.err.println("ERROR WHILE WRITTING TO LOG FILE!!!");
					localIOException2.printStackTrace();
					closeLog();
				}
			} catch (Exception localException) {
				System.err.println("ERROR WHILE WRITTING TO LOG FILE!!!");
				localException.printStackTrace();
				closeLog();
			}
		}
	}

	public void printError(Object paramObject) {
		if (MODE == 1) {
			System.err.println(paramObject);
		} else if (MODE == 0) {
			println(paramObject);
		}
	}

	public void dump(byte[] paramArrayOfByte) {
		if ((MODE == 1) || (MODE == 0)) {
			dump(paramArrayOfByte, paramArrayOfByte.length);
		}
	}

	public void dump(byte[] paramArrayOfByte, int paramInt) {
		if ((MODE == 1) || (MODE == 0)) {
			dump(paramArrayOfByte, 0, paramInt);
		}
	}

	public void dump(byte[] paramArrayOfByte, int paramInt1, int paramInt2) {
		if ((MODE == 1) || (MODE == 0)) {
			if (paramArrayOfByte.length - paramInt1 < paramInt2) {
				println("Invalid buffer");
				return;
			}

			StringBuffer localStringBuffer = new StringBuffer(2 * paramInt2);

			for (int i = paramInt1; i < paramInt1 + paramInt2; i++) {
				appendHexPair(paramArrayOfByte[i], localStringBuffer);
			}

			for (int i = 0; i < localStringBuffer.length(); i += 60) {
				int j = i + 60;
				if (j > localStringBuffer.length()) {
					j = localStringBuffer.length();
				}

				println(localStringBuffer.substring(i, j));
			}
		}
	}

	public String dumpIPMI(byte[] paramArrayOfByte, int paramInt1, int paramInt2) {
		String str1 = "";

		if (paramArrayOfByte.length - paramInt1 < paramInt2) {
			println("Invalid buffer");
			return null;
		}

		StringBuffer localStringBuffer = new StringBuffer(2 * paramInt2);

		for (int i = paramInt1; i < paramInt1 + paramInt2; i++) {
			appendHexPair(paramArrayOfByte[i], localStringBuffer);
		}
		String str2 = localStringBuffer.substring(3, localStringBuffer.length());
		for (int j = 0; j < str2.length(); j += 48) {
			int k = j + 48;
			if (k > str2.length()) {
				k = str2.length();
			}
			str1 = str1 + str2.substring(j, k) + "\n";
		}
		return str1;
	}

	public static void appendHexPair(byte paramByte, StringBuffer paramStringBuffer) {
		char c1 = kHexChars[((paramByte & 0xF0) >> 4)];
		char c2 = kHexChars[(paramByte & 0xF)];

		paramStringBuffer.append(c1);
		paramStringBuffer.append(c2);
		paramStringBuffer.append(' ');
	}

	public String getTimeStamp() {
		String str = "";
		Date localDate = new Date();
		SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("'on_'dd-MM-yyyy'_at_'HH-mm-ss-SSS");
		str = localSimpleDateFormat.format(localDate);
		return str;
	}

	public void closeLog() {
		try {
			if (this.pWriter != null)
				this.pWriter.close();
			this.pWriter = null;
			if (this.fWriter != null)
				this.fWriter.close();
			this.fWriter = null;
			if (this.runTimeLogging) {
				InfoDialog.showDialog(JViewer.getMainFrame(), LocaleStrings.getString("AH_1_DBG") + this.logFile,
						LocaleStrings.getString("AH_2_DBG"), 1);

				this.runTimeLogging = false;
			}
		} catch (IOException localIOException) {
			System.err.println("ERROR WHILE CLOSING LOG FILE WRITTER!!!");
			this.pWriter = null;
			this.fWriter = null;
			localIOException.printStackTrace();
		}
	}

	private static final char[] kHexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
			'F' };

	public void initDebugLogFileChooser() {
		DebugLogFileChooser localDebugLogFileChooser = new DebugLogFileChooser();
		initLogFile();
		localDebugLogFileChooser.start();
	}

	public void initLog(File paramFile) {
		if (paramFile == null) {
			initLogFile();
		} else {
			this.logFile = paramFile;
			this.runTimeLogging = true;
		}
		try {
			this.fWriter = new FileWriter(this.logFile, true);
		} catch (IOException localIOException) {
			System.err.println("ERROR WHILE CREATING LOG FILE!!!");
			localIOException.printStackTrace();
			this.fWriter = null;
		}
		MODE = 0;
	}

	public void initLogFile() {
		this.logFile = new File(System.getProperty("user.home") + File.separator + "JViewer_log_to-" + JViewer.getIp()
				+ "-" + getTimeStamp() + ".log");
	}

	public File getLogFile() {
		return this.logFile;
	}

	public boolean isRunTimeLogging() {
		return this.runTimeLogging;
	}

	public void setRunTimeLogging(boolean paramBoolean) {
		this.runTimeLogging = paramBoolean;
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\Debug.class Java
 * compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */